#include <stdio.h>
//
char* my_strstr(const char* str1, const char* str2)
{
	if (!*str2)
		return str1;
	while (*str1 != '\0')
	{
		char* tmp1 = str1;
		char* tmp2 = str2;
		if (*tmp1== *tmp2)
		{
			do
			{
				tmp1++;
				tmp2++;
			} while (*tmp1 == *tmp2&& *tmp2 !='\0');
			if (*tmp2 == '\0')
				return str1;
		}
		str1++;
	}
	return NULL;
}
//int main()
//{
//	char str1[] = "cbdaskdjk";
//	char str2[] = "sad";
//	if (my_strstr(str1, str2) == NULL)
//		printf("NULL");
//	return 0;
//}
//#include <stdio.h>
char* my_strncpy(char* def, const char* sourc, size_t num)
{
	char* tmp = def;
	for (int i = 0; i < num; i++)
	{
		*def = *sourc;
		def++;
		sourc++;
	}
	return tmp;
}
//int main()
//{
//	char str[] = "cbda";
//	char def[8] = { 0 };
//	printf(my_strncpy(def, str, 2));
//}
//#include <stdio.h>
char* my_strncat(char* def, const char* sourc, size_t num)
{
	char* tmp = def;
	while (*tmp != '\0')
	{
		tmp++;
	}
	for (int i = 0; i < num; i++)
	{
		*tmp = *sourc;
		tmp++;
		sourc++;
	}
	return def;
}
//int main()
//{
//	char str[20] = "bcajd";
//	char sourc[] = "dad";
//	printf(my_strncat(str, sourc, 2));
//}
//#include <stdio.h>
//
void* my_memcpy(void* def, const void* sourc, size_t num)
{
	void * tmp = def;
	for (int i = 0; i < num; i++)
	{
		*(char*)tmp = *(char*)sourc;
		((char*)tmp)++;
		((char*)sourc)++;
	}
	return def;
}
//int main()
//{
//	int arr[] = { 1,2,3,4,5,6 };
//	int str[10] (= { 0 };
//	printf("%d", ((int*)my_memcpy(str, arr, 3 * sizeof(int)))[0]);
//	return 0;
////}
//#include <stdio.h>
void* my_memmove(void* def, const void* sourc, size_t num)
{
	void* tmp = def;
	if (def > sourc)
	{
		for (int i = 0; i < num-1; i++)
		{
			*((char*)def) = *((char*)sourc);
			((char*)def)++;
			((char*)sourc)++;
		}
	}
	else
	{
		char* newdef = ((char*)def) + num - 1;
		char* newsourc = ((char*)sourc) + num - 1;
		for (int i = num-1; i > 0; i--)
		{
			*newdef = *newsourc;
			newdef--;
			newsourc--;
		}
	}
	return tmp;
}
//int main()
//{
//	int str[5] = { 1,3,4,5 ,6};
//	my_memmove(str+2, str, 5 * sizeof(int));
//	printf("%d", str[4]);
//	return 0;
//}
